class GalleriesController < ApplicationController

  include ApplicationHelper

  require 'date'

  require 'rubygems'

  require 'util/imagesize'


	def index
    @config = get_config
    
		@galleries = Gallery.find(:all, :limit => 20, :order => "added DESC")
    
    @images = Array.new
   
    @galleries.each do |gallery|
      
      fullpath = @config['image-root'] + "/" + gallery.imagedir
      
      file = String.new
      if File.exists?(fullpath+"/thumb.jpg")
        file = "thumb.jpg"
      elsif gallery.thumbfile?
        file = gallery.thumbfile
      elsif gallery.thumbindex?
        file = Dir.entries(fullpath)[gallery.thumbindex+1]
      else
        file = Dir.entries(fullpath)[2]
      end
      
      if !file.starts_with?(".")
       
        image = Hash.new      


	   File.open(fullpath+"/"+file, "rb") do |fh|
          size = ImageSize.new(fh.read).size
          image["size"] = size;
        end	
  
        image["path"] = gallery.imagedir + "/" + file
        image["id"] = gallery.id
        
        @images << image
      end
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @galleries }
    end
	end
  
  def show
    @config = get_config
    @gallery = Gallery.find(params[:id])
    
    @images = Array.new
    fullpath = @config['image-root'] + "/" + @gallery.imagedir
    
    thumbfile = @gallery.thumbfile

    Dir.foreach(fullpath) do |file|
      if !file.starts_with?(".") and (thumbfile.nil? or !file.starts_with(thumbfile))
       
        image = Hash.new
        File.open(fullpath+"/"+file, "rb") do |fh|
          size = ImageSize.new(fh.read).size
          image["size"] = size;
        end
        
        image["path"] = @gallery.imagedir + "/" + file
        @images << image
      end
    end

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @gallery }
    end
  end
  
  
  def new
    @config = get_config
    @auth = params[:auth]
    @gallery = Gallery.new
    @categories = Category.find(:all)
    @sites = Site.find(:all)

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @gallery }
    end
  end
  
  def create
    @config = get_config
    @gallery = Gallery.new(params[:gallery])
    @auth = params[:auth]
    
    if @auth.nil? or !@auth.eql?(@config['auth'])
      redirect_to :action => "new"
      return
    end
    
    if @gallery.name.empty?
      @gallery.name = nil
    end
    
    if @gallery.thumbfile.nil? or @gallery.thumbfile.empty?
      @gallery.thumbfile = nil
    end
    
    if (!params[:zip].empty?)
            
      params[:zip] = params[:zip].strip
      filename = File.basename(params[:zip],".zip")
      
      imageroot = @config['image-root']
      imageroot += "/" + @gallery.site.name
      
      dcommand = @config['download-command']
      dcommand = dcommand.sub("[location]", imageroot)
      dcommand = dcommand.sub("[url]", params[:zip])
      puts dcommand
      system(dcommand) 
      
      unzipcommand = @config['unzip-command']
      unzipcommand = unzipcommand.sub("[location]", imageroot+"/"+filename)
      unzipcommand = unzipcommand.sub("[file]", imageroot+"/"+filename+".zip")
      puts unzipcommand
      system(unzipcommand) 
      
      File.delete(imageroot+"/"+filename+".zip")
      
      @gallery.imagedir = @gallery.site.name+"/"+filename
    else 
      if @gallery.imagedir.nil?
        redirect_to :action => "new", :auth => @config['auth']
        return
      end
    end

    added = params[:added]
    if added.include? "-"
      @gallery.added = Date.strptime(added, '%Y-%m-%d')
    else
      @gallery.added = Time.now
    end
    @gallery.save

    redirect_to :action => "new", :auth => @config['auth']
  end
  
  
  

end
